package light.head.dao.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.Memorandum;

import org.springframework.stereotype.Service;

@Service
public class MemorandumDAO extends BaseDAO<Memorandum, Integer> {

	/**
	 * 根据机构编号和角色编号获得日程信息
	 */
	public List<Memorandum> findByOcAndCu(Integer oc, Integer cu){
		if(oc == null || cu == null) return null;
		String hql = " FROM Memorandum memo WHERE memo.oc = ? AND memo.cu = ?";
		return findI(hql, oc, cu);
	}
		

	/**
	 * 根据查询条件分页查询权限信息
	 * @param	查询条件
	 * @param	分页信息
	 */
	public RESULT findByMap(Integer oc, Map<String, String> map, PageBean pageBean){
		StringBuffer hql = new StringBuffer(" FROM Memorandum memo WHERE 1 = 1 ");
		List<Object> list = null;
		if(null != map){
			list = new ArrayList<Object>();
			String title = map.get(Parm.TITLE);
			String start = map.get(Parm.START);
			String end = map.get(Parm.END);
			String designee = map.get(Parm.DESIGNEE);
			String transactor = map.get(Parm.TRANSATOR);
			String taskStatus = map.get(Parm.TASKSTATUS);
			if(isNotNull(title)) {
				hql.append(" AND memo.title LIKE % ?");
				list.add(title);
				hql.append("%");
			}if(isNotNull(start)) {
				hql.append(" AND memo.start > ?");
				list.add(DateUtils.getDateByStr(start, "yyyy-MM-dd"));
			}if(isNotNull(end)) {
				hql.append(" AND memo.end < ?");
				list.add(DateUtils.getDateByStr(end, "yyyy_MM-dd"));
			}if(isNotNull(designee)) {
				hql.append(" AND memo.designee = ?");
				list.add(Integer.parseInt(designee));
			}if(isNotNull(transactor)) {
				hql.append(" AND memo.transactor = ?");
				list.add(Integer.parseInt(transactor));
			}if(isNotNull(taskStatus)) {
				hql.append(" AND memo.taskStatus = ?");
				list.add(Integer.parseInt(taskStatus));
			}
		}
			hql.append(" AND memo.deleted = 0 OR memo.deleted = null");
			hql.append(" ORDER BY memo.code");
			
			if(findToPageBean(hql.toString(), pageBean, list) == RESULT.SUCCESS)
				return RESULT.SUCCESS;
				
		return RESULT.ERROR;
	}
	
	public RESULT update(Memorandum memo){
		if(memo == null || memo.getCode() == null) return null;
		
		Memorandum old = get(memo.getCode());
		
		if(old == null) return RESULT.NOT_FIND;
		
		if(memo.getTitle() != null){
			old.setTitle(memo.getTitle());
		}
		if(memo.getStart() != null && memo.getStart() != old.getStart()){
			old.setStart(memo.getStart());
		}
		if(memo.getEnd() != null && memo.getEnd() != old.getEnd()){
			old.setEnd(memo.getEnd());
		}
		if(memo.getContent() != null){
			old.setContent(memo.getContent());
		}
		if(memo.getContent() != null){
			old.setContent(memo.getContent());
		}
		if(memo.getCu() != null){
			old.setCu(memo.getCu());
		}
		if(null != memo.getDesignee() && memo.getDesignee() > 0) {
			old.setDesignee(memo.getDesignee());
		}
		if(null != memo.getTransactor() && memo.getTransactor() > 0) {
			old.setTransactor(memo.getTransactor());
		}
		if(null != memo.getTaskStatus() && memo.getTaskStatus() > 0) {
			old.setTaskStatus(memo.getTaskStatus());
		}
		if(isNotNull(memo.getRemark())) {
			old.setRemark(memo.getRemark());
		}
		return upd(old);
	}
}
